#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, fa[50001];
struct Node {
	ll x, y, v;
	bool operator < (const Node& A) {
		return v < A.v;
	}
} a[100001];

ll findset(ll i) {
	if (i == fa[i])
		return i;
	return fa[i] = findset(fa[i]);
}

inline void Kruskal() {
	for (ll i = 1; i <= n; ++i)
		fa[i] = i;
	sort(a + 1, a + 1 + m);
	ll ans = 0;
	for (ll i = 1; i <= m; ++i) {
		ll x = findset(a[i].x), y = findset(a[i].y);
		if (x != y) {
			fa[x] = y;
			ans += a[i].v;
		}
	}
	printf("%lld\n", ans);
}

int main() {
	scanf("%lld%lld", &n, &m);
	for (ll i = 1; i <= m; ++i)
		scanf("%lld%lld%lld", &a[i].x, &a[i].y, &a[i].v);
	Kruskal();
	return 0;
}
